package com.coderman.business.service.imp;

import com.coderman.business.mapper.StudentClassMapper;
import com.coderman.business.service.IStudentClassService;
import com.coderman.business.service.base.AbstractServiceImpl;
import com.coderman.common.error.SystemCodeEnum;
import com.coderman.common.error.SystemException;
import com.coderman.common.model.student.Student;
import com.coderman.common.model.student.StudentClass;
import com.coderman.common.vo.student.StudentClassVO;
import com.coderman.common.vo.system.PageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生班级关系业务层实现
 * @author  文晓平
 * @Date 2023-1-9
 * @version 1.0
 */

@Service
public class StudentClassServiceImpl extends AbstractServiceImpl<StudentClassMapper, StudentClass, Integer> implements IStudentClassService {
    @Autowired
    private StudentClassMapper studentClassMapper;
    @Override
    public PageVO<StudentClass> findList(Integer pageNum, Integer pageSize, StudentClassVO studentClassVO) {
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Student.class);
        List<StudentClass> studentClasses= studentClassMapper.selectByExample(o);
        PageInfo<StudentClass> info=new PageInfo<>(studentClasses);
        return new PageVO<>(info.getTotal(),studentClasses);
    }

    /**
     * 添加班级学员关系
     *
     * @param studentClassVO
     */
    @Override
    public void add(StudentClassVO studentClassVO) {
        StudentClass studentClass = new StudentClass();
        BeanUtils.copyProperties(studentClassVO,studentClass);
        studentClassMapper.insert(studentClass);
    }

    /**
     * 删除班级学员关系
     *
     * @param id
     */
    @Override
    public void delete(Integer id) throws SystemException {
        StudentClass studentClass = studentClassMapper.selectByPrimaryKey(id);
        if(studentClass==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除信息不存在");
        }
        studentClassMapper.deleteByPrimaryKey(id);
    }

    /**
     * 所有班级学员关系信息
     *
     * @return
     */
    @Override
    public List<StudentClassVO> findAllVO() {
        List<StudentClass> studentClassList = studentClassMapper.selectAll();
        //转vo
        List<StudentClassVO> studentClassVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(studentClassList)) {
            for (StudentClass studentClass : studentClassList) {
                StudentClassVO s = new StudentClassVO();
                BeanUtils.copyProperties(studentClass, s);
                Example o = new Example(StudentClass.class);
                o.createCriteria().andEqualTo("stuClsId", studentClass.getStuClsId())
                        .andNotEqualTo("type", 0);
                studentClassVOS.add(s);
            }
        }
        return studentClassVOS;
    }

    /**
     * 全部班级学员关系信息
     *
     * @return
     */
    @Override
    public List<StudentClass> findAll() {
        return studentClassMapper.selectAll();
    }
}
